package com.itboye.hutouben.activity.mysetting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * 通过旧密码修改密码
 */
public class ModifyPassActivity extends BaseActivity implements Observer{
    TextView add_shap_title_tv;
    ImageView close_icon;
    EditText modify_old,modify_new,modify_onew_two;
    TextView modify_ok;

    UserPresenter userPresenter;
    String uid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pass);

        add_shap_title_tv.setText(R.string.update_pass);
        uid=(String) SPUtils.get(this,null, Const.ID,"");
        userPresenter=new UserPresenter(this);
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.modify_ok:
                if(isCheckData()){
                    showProgressDialog("修改中,请稍后", true);
                    userPresenter.modifyPass(uid,modify_old.getText().toString().trim(),modify_new.getText().toString().trim());
                }

                break;

            default:
                break;
        }
    }
    /**
     * 输入合法性验证
     * @return
     */
    private boolean isCheckData() {
        String old=modify_old.getText().toString().trim();
        String new_one=modify_new.getText().toString().trim();
        String new_two=modify_onew_two.getText().toString().trim();
        if(TextUtils.isEmpty(old)){
            ByAlert.alert("旧密码不能为空");
            return false;
        }
        if(TextUtils.isEmpty(new_one) || TextUtils.isEmpty(new_two)){
            ByAlert.alert("新密码不能为空");
            return false;
        }
        if (old.length()< 6 || new_one.length()< 6 || new_two.length()< 6) {
            ByAlert.alert("密码不能少于6位");
            return false;
        }
        if (!new_one.equals(new_two)) {
            ByAlert.alert("两次密码输入不一致");
            return false;
        }
        return true;
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if(entity!=null){
            IsUtilUid.tianjia(entity);
            if(entity.getEventType()==UserPresenter.modify_pass_success){
                ByAlert.alert(entity.getData());
            }
            if(entity.getEventType()==UserPresenter.modify_pass_fail){
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
